package cn.tedu.boot42.controller;

import cn.tedu.boot42.mapper.UserMapper;
import cn.tedu.boot42.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    @Autowired(required = false)
    UserMapper mapper;

    @RequestMapping("/check")
    public int check(String username){
        User user = mapper.selectByUsername(username);
        return user==null?1:2;//代码可读性比if else较低
    }

    @RequestMapping("/reg")
    public int reg(User user){
        User u =
                mapper.selectByUsername(user.getUsername());
        if (u!=null){
           return 2;//代表用户名已经存在
        }
        mapper.insert(user);
        return 1;//注册成功!
    }

    @RequestMapping("/login")
    public int login(User user){
        User u = mapper.selectByUsername(user.getUsername());
        if (u != null){
            //拿用户输入的密码和数据库中的密码比较
            if (u.getUsername().equals(user.getPassword())){
                return 1;//登录成功
            }
            return 3;//密码错误
        }
        return 2;//用户名不存在
    }
}
